<?php

namespace Plu\Library\Models\Platform\Permission;

use Phalcon\Mvc\Model;

/**
 * 获取当前用户是否有此权限
 */
class PermissionRoleOfUser extends Model {
    const CACHE_TIME = 60 * 3;

    public $Id;

    public $RoleId;

    public $UserId;

    public $WriteTime;

    public function getSource() {
        return 'PermissionRoleOfUser';
    }

    public function initialize() {
        $this->setReadConnectionService('db.platform.slave');
    }

    /**
     * 获取用户及权限列表
     */
    static public function getRoles($uid) {
        $users = self::find(array(
            'columns' => 'UserId, RoleId',
            'cache'   => array(
                'key'	   => 'roleofusers',
                'lifetime' => self::CACHE_TIME
            )
        ));

        $user = $users->filter(function($item) use ($uid) {
            if ($item->UserId == $uid) {
                return $item;
            }
        });

        $roles = array();
        if ( count($user) > 0 )  {
            for ($c = 0, $len = count($user); $c < $len; $c++) {
                $roles[] = $user[$c]->RoleId;
            }
            return $roles;
        } else {
            return false;
        }
    }
}

?>
